package cn.com.nes.site.service.project.impl;


import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.mybatis.agent.crm.entity.Opportunity;
import cn.com.nes.mybatis.agent.project.entity.ProjectCustomerRelationship;
import cn.com.nes.mybatis.agent.project.entity.TProject;
import cn.com.nes.mybatis.agent.project.mapper.TProjectMapper;
import cn.com.nes.site.entity.vo.project.SelectBusinessOpportunityAndSaveCustomerVO;
import cn.com.nes.site.service.crm.opportunity.CrmOpportunityService;
import cn.com.nes.site.service.project.ProjectCustomerRelationshipService;
import cn.com.nes.site.service.project.ProjectWorkOrderDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProjectWorkOrderDataServiceImpl extends ServiceImpl<TProjectMapper, TProject> implements ProjectWorkOrderDataService {

    @Resource
    ProjectCustomerRelationshipService projectCustomerRelationshipService;

    @Resource
    CrmOpportunityService crmOpportunityService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void selectBusinessOpportunityAndSaveCustomer(SelectBusinessOpportunityAndSaveCustomerVO vo) throws Exception {
        try {
            List<String> opportunityIds = vo.getOpportunityIdList();
            List<ProjectCustomerRelationship> projectCustomerRelationshipList = new ArrayList<>();
            ProjectCustomerRelationship projectCustomerRelationship = null;
            for (String opportunityId : opportunityIds) {
                if(opportunityId.equals(vo.getMainBusinessId())){
                    throw new BusinessException("本商机不能关联自己");
                }
                Opportunity opportunity = crmOpportunityService.getOpportunityDetailByUuid(opportunityId);
                projectCustomerRelationship = new ProjectCustomerRelationship();
                projectCustomerRelationship.setCustomerId(opportunity.getCustomerId());
                projectCustomerRelationship.setBusinessId(opportunityId);
                projectCustomerRelationship.setProjectUuid(vo.getProjectUuid());
                if (projectCustomerRelationshipService.getRelationshipsExcludeMain(vo.getProjectUuid(), opportunity.getCustomerId()).isEmpty()) {
                    projectCustomerRelationship.setMainCustomerId(vo.getMasterCustomerId());
                    projectCustomerRelationship.setMainFlag(0);
                    projectCustomerRelationship.setImportBatch(vo.getOperateTimeStamp());
                    projectCustomerRelationship.setDataType(2);
                    projectCustomerRelationship.setDeleted(0);
                    projectCustomerRelationship.setMainBusinessId(vo.getMainBusinessId());
                    projectCustomerRelationship.setGmtCreateId(vo.getOperateUserId());
                    projectCustomerRelationship.setGmtCreate(vo.getOperateDate());
                    projectCustomerRelationshipList.add(projectCustomerRelationship);
                } else {
                    throw new BusinessException("数据已存在商机名称:"+opportunity.getOpportunityName());
                }
            }
            projectCustomerRelationshipService.saveOrUpdateBatch(projectCustomerRelationshipList);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }
}